﻿using Omu.ValueInjecter;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace BaiTu.Common.Helpers
{
    /// <summary>
    /// 类型转换helper 
    /// </summary>
    public sealed class TypeConverterHelper : Framework.Interfaces.ISingletonDependency
    {
        /// <summary>
        /// int 类型转换为string类型
        /// </summary>
        /// <param name="iValue">int类型的值</param>
        /// <returns>返回值 转换后的string</returns>
        public static string IntToStr(int iValue)
        {
            return Convert.ToString(iValue);
        }
        /// <summary>
        /// 字符串换换为int类型 转换失败是否抛出异常信息 如果不抛出异常信息 则int为-1
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="fauseThrowException">转换失败是否抛出异常信息 如果不抛出异常信息 则int为-1</param>
        /// <returns></returns>
        public static int StrToInt(string str, bool fauseThrowException)
        {
            int result = -1;
            try
            {
                result = Convert.ToInt32(str);
            }
            catch (Exception ex)
            {
                if (fauseThrowException == true)
                {
                    throw new Framework.Exceptions.BaiTuCoreException(string.Format("字符串转换成为int失败，字符串为:{0}", str));
                }
                else
                {
                    result = -1;
                }
            }
            return result;
        }
        /// <summary>
        /// 字符串换换为int类型 转换失败是否抛出异常信息 如果不抛出异常信息 则int为-1
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="fauseThrowException">转换失败是否抛出异常信息 如果不抛出异常信息 则int为-1</param>
        /// <returns></returns>
        public static int StrToInt(string str, int defaultValue, bool fauseThrowException)
        {
            int result = -1;
            try
            {
                result = Convert.ToInt32(str);
            }
            catch (Exception ex)
            {
                if (fauseThrowException == true)
                {
                    throw new Framework.Exceptions.BaiTuCoreException(string.Format("字符串转换成为int失败，字符串为:{0}", str));
                }
                else
                {
                    result = defaultValue;
                }
            }
            return result;
        }

        /// <summary>
        /// 字符串换换为int类型 转换失败是否抛出异常信息 如果不抛出异常信息 则int为-1
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="fauseThrowException">转换失败是否抛出异常信息 如果不抛出异常信息 则int为-1</param>
        /// <returns></returns>
        public static float StrToFloat(string str, float defaultValue, bool fauseThrowException)
        {
            float result = -1;
            try
            {
                result = Convert.ToSingle(str);
            }
            catch (Exception ex)
            {
                if (fauseThrowException == true)
                {
                    throw new Framework.Exceptions.BaiTuCoreException(string.Format("字符串转换成为int失败，字符串为:{0}", str));
                }
                else
                {
                    result = defaultValue;
                }
            }
            return result;
        }

        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(object Expression)
        {
            return ValidatorHelper.IsNumeric(Expression);
        }

        /// <summary>
        /// 将全角数字转换为数字
        /// </summary>
        /// <param name="SBCCase"></param>
        /// <returns></returns>
        public static string SBCCaseToNumberic(string SBCCase)
        {
            char[] c = SBCCase.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
                if (b.Length == 2)
                {
                    if (b[1] == 255)
                    {
                        b[0] = (byte)(b[0] + 32);
                        b[1] = 0;
                        c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
                    }
                }
            }
            return new string(c);
        }
        /// <summary>
        /// 将字符串转换为Color
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static Color ToColor(string color)
        {
            int red, green, blue = 0;
            char[] rgb;
            color = color.TrimStart('#');
            color = Regex.Replace(color.ToLower(), "[g-zG-Z]", "");
            switch (color.Length)
            {
                case 3:
                    rgb = color.ToCharArray();
                    red = Convert.ToInt32(rgb[0].ToString() + rgb[0].ToString(), 16);
                    green = Convert.ToInt32(rgb[1].ToString() + rgb[1].ToString(), 16);
                    blue = Convert.ToInt32(rgb[2].ToString() + rgb[2].ToString(), 16);
                    return Color.FromArgb(red, green, blue);
                case 6:
                    rgb = color.ToCharArray();
                    red = Convert.ToInt32(rgb[0].ToString() + rgb[1].ToString(), 16);
                    green = Convert.ToInt32(rgb[2].ToString() + rgb[3].ToString(), 16);
                    blue = Convert.ToInt32(rgb[4].ToString() + rgb[5].ToString(), 16);
                    return Color.FromArgb(red, green, blue);
                default:
                    return Color.FromName(color);

            }
        }

        /// <summary>
        /// 将字符串转换成为byte数组
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static byte[] StringToByte(string source)
        {
            source.MustNotNull("StringToByte字符串转换为数组，字符串不能为null");
            return System.Text.Encoding.UTF8.GetBytes(source);
        }

        /// <summary>
        /// 将byte数组转换成为string
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ByteToString(byte[] bytes, bool isNullThrowException = false)
        {
            if (isNullThrowException == true)
            {
                bytes.MustNotNull("byte数组转换成为string，byte数组为null");
            }
            return System.Text.Encoding.UTF8.GetString(bytes);
        }

      

        /// <summary>
        /// 类型转换 将一个源类型转换为目标类型  
        /// </summary>
        /// <param name="objDest"></param>
        /// <param name="objSource"></param>
        public static void ConvertSourceToDest(object objDest, params object[] objSource)
        {
            if (objSource == null || objSource.Length == 0 || objDest == null)
            {
                objDest = null;
            }
            else
            {
                Omu.ValueInjecter.ValueInjecter injecter = new Omu.ValueInjecter.ValueInjecter() { };
                injecter.Inject<ConvertConvention>(objDest, objSource);
            }
        }
    }
    /// <summary>
    /// 不区分大小写的约定
    /// </summary>
    public class ConvertConvention : ConventionInjection
    {
        protected override bool Match(ConventionInfo c)
        {
            return c.SourceProp.Name.Equals(c.TargetProp.Name, StringComparison.CurrentCultureIgnoreCase) && (c.SourceProp.Type == c.TargetProp.Type);
        }


    }
}
